#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Drawing;
using System.Windows.Forms;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Managers;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class GamesList : Form
	{
		private ManualNewGame _manualNewGame;

		#region Constructor

		public GamesList()
		{
			InitializeComponent();
			InitializeRequiredSystems();
		}

		#endregion Constructor

		#region Populate Games List

		private void PopulateGamesList()
		{
			txtGameId.Text = "";
			txtName.Text = "";
			txtPublisher.Text = "";
			txtContent.Text = "";
			txtRating.Text = "";
			txtFileName.Text = "";
			txtInternalName.Text = "";
			txtPath.Text = "";
			txtProductName.Text = "";
			txtCompany.Text = "";
			txtDateAdded.Text = "";
			txtWasChecked.Text = "";
			txtDateChecked.Text = "";

			lstGamesList.DataSource = GamesDAO.GetGameList();
		}

		#endregion Populate Games List

		#region Initialize Required Systems

		private void InitializeRequiredSystems()
		{
		}

		#endregion Initialize Required Systems

		#region GamesList_Load

		private void GamesList_Load(object sender, EventArgs e)
		{
			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 new Bitmap(Resources.B256x256_Games, new Size(48, 48)),
			                                                 "GShield Games List",
			                                                 "Games Active within the GShield System");

			PopulateGamesList();
		}

		#endregion GamesList_Load

		#region btnClose_Click

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		#endregion btnClose_Click

		#region btnRefresh_Click

		private void btnRefresh_Click(object sender, EventArgs e)
		{
			PopulateGamesList();
		}

		#endregion btnRefresh_Click

		#region btnDeleteSelectedGame_Click

		private void btnDeleteSelectedGame_Click(object sender, EventArgs e)
		{
			if (lstGamesList.SelectedItem != null && lstGamesList.SelectedItem.ToString().Trim().Length > 0)
			{
				if (
					MessageBox.Show(
						"Are your sure you want to remove the game " + lstGamesList.SelectedItem + " from GShields database?",
						"Remove Game Data", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
				{
					if (GamesDAO.GetGameById(int.Parse(txtGameId.Text)) != null)
					{
						GamesDAO.Delete(int.Parse(txtGameId.Text));

						Logging.Write("The game, " + txtName.Text + " has been deleted.", LogCategory.Games, LogPriority.Medium,
						              LogType.GameDelete);

						PopulateGamesList();

						MessageBox.Show("The game has been deleted.", "Delete Game Data", MessageBoxButtons.OK,
						                MessageBoxIcon.Information);
					}
				}
			}
			else
			{
				MessageBox.Show("You must select a game from the list to delete.", "Delete Game", MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}
		}

		#endregion btnDeleteSelectedGame_Click

		#region lstGamesList_SelectedIndexChanged

		private void lstGamesList_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstGamesList.SelectedItem != null && lstGamesList.SelectedItem.ToString().Trim().Length > 0)
			{
				Game g = GamesDAO.GetGameByName(lstGamesList.SelectedItem.ToString());

				if (g != null)
				{
					txtGameId.Text = g.Id.ToString();
					txtName.Text = g.Name;
					txtPublisher.Text = g.Publisher;
					txtContent.Text = g.Content;
					txtRating.Text = g.Rating;
					txtFileName.Text = g.FileName;
					txtInternalName.Text = g.InternalName;
					txtPath.Text = g.Path;
					txtProductName.Text = g.ProductName;
					txtCompany.Text = g.Company;
					txtDateAdded.Text = g.DateAdded.ToString();
					txtWasChecked.Text = g.WasChecked.ToString();
					txtDateChecked.Text = g.DateChecked.ToString();
				}
			}
			else
			{
				txtGameId.Text = "";
				txtName.Text = "";
				txtPublisher.Text = "";
				txtContent.Text = "";
				txtRating.Text = "";
				txtFileName.Text = "";
				txtInternalName.Text = "";
				txtPath.Text = "";
				txtProductName.Text = "";
				txtCompany.Text = "";
				txtDateAdded.Text = "";
				txtWasChecked.Text = "";
				txtDateChecked.Text = "";
			}
		}

		#endregion lstGamesList_SelectedIndexChanged

		#region btnManuallyAddGame_Click

		private void btnManuallyAddGame_Click(object sender, EventArgs e)
		{
			if (_manualNewGame == null || _manualNewGame.IsDisposed)
				_manualNewGame = new ManualNewGame();

			if (_manualNewGame.Visible == false)
				_manualNewGame.Show();
		}

		#endregion btnManuallyAddGame_Click

		#region btnHelp_Click

		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(this, Settings.Default.HelpFileName, HelpNavigator.TopicId, Settings.Default.HelpTopics_GamesForm);
		}

		#endregion btnHelp_Click

		#region btnChangeRating_Click

		private void btnChangeRating_Click(object sender, EventArgs e)
		{
			if (txtGameId.Text.Length <= 0)
			{
				MessageBox.Show("You must select a game from the list to update.", "Update Game", MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}
			else
			{
				if (GamesDAO.GetGameById(int.Parse(txtGameId.Text)) != null)
				{
					WindowManager.AddWindow(new ChangeRating(GamesDAO.GetGameById(int.Parse(txtGameId.Text))), false);
				}
				else
				{
					MessageBox.Show(this, "Game does not exist, cannot modify rating.");
				}
			}
		}

		#endregion btnChangeRating_Click
	}
}